//
//  CNComposeTextView.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/31.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit

class CNComposeTextView: UITextView {
    
    override var text: String? {
        didSet {
            placeLabel.hidden = self.hasText()
        }
    }
    override var font: UIFont? {
        didSet {
            placeLabel.font = font
        }
    }
    var placeText: String? {
        didSet {
            placeLabel.text = placeText
        }
    }
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        prepareUI()
//        self.delegate = self
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "TextDidChange:", name: UITextViewTextDidChangeNotification, object: self)
    }
    func TextDidChange(notification: NSNotification) {
        placeLabel.hidden = hasText()
    }
    private func prepareUI() {
        addSubview(placeLabel)
        placeLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: placeLabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 5))
        addConstraint(NSLayoutConstraint(item: placeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 8))
    }    
    private lazy var placeLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(18)
        label.textColor = UIColor.lightGrayColor()
        label.sizeToFit()
        return label
    }()
}
